package ru.spb.bragnikita.myevents.composite;

import javax.enterprise.context.ApplicationScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Nikita.Brazhnikov
 */
@Named("user_verif")
@ApplicationScoped
public class UserVerificationBean {

	private static final Logger logger = Logger.getLogger(UserVerificationBean.class.getName());

	private Map<String, User> usersCache = new ConcurrentHashMap<String, User>(10);

	public UserVerificationBean() {
		usersCache.put("Nikita", new User("Nikita", "secret"));
	}

	public STATUS verify(String username, String password) {
		User user = usersCache.get(username);
		if (user == null)
			return STATUS.NOT_FOUND;
		if (!user.getPassword().equals(password))
			return STATUS.INCORRECT_PASSWORD;
		return STATUS.OK;
	}

	public void validate(ActionEvent event) {
		logger.log(Level.INFO, "Someone is trying to login");
	}

	public enum STATUS {
		NOT_FOUND, INCORRECT_PASSWORD, OK;
	}

	private class User {
		private String name;
		private String password;

		private User(String name, String password) {
			this.name = name;
			this.password = password;
		}

		public String getName() {
			return name;
		}

		public String getPassword() {
			return password;
		}
	}

}
